<?
$pageTitle = "Client Information";
if (isset($_POST['clientID'])) {
	$clientID = $HTTP_POST_VARS['clientID'];
	createSessionClientId($clientID);
	print_r($_POST);
}
elseif (isset($_GET['clientID'])) {
	$clientID = $HTTP_GET_VARS['clientID'];
	createSessionClientId($clientID);
}
elseif (isset($_SESSION['session_clientId'])) {
	$clientID = $_SESSION['session_clientId'];
}

?>
<SCRIPT LANGUAGE="JavaScript">
<!--
function OpenWindow(url, name, width, height)
{
	popupWin = window.open(url, '', 'width=' + width + ', height=' + height + ',scrollbars,resizable')
}
-->
</script>
<?

$myArray = array();

if ($HTTP_POST_VARS)
$myArray = $HTTP_POST_VARS;

if (isset($clientID))	{
	$query = "SELECT * FROM er_Client WHERE client_id ='$clientID'";
	$result = r_db($query);
	$client_record = mysql_num_rows($result);
}
if (isset($client_record)) {
	while($row = mysql_fetch_array($result)) {
		print "<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><tr><td valign=\"top\"><h1>$pageTitle</h1></td><td align=\"right\" valign=\"top\">";
		$i = 0;
		print "<a href=\"?page=visit_details&ClientID=$clientID\"><img border=\"0\" src=\"images/ViewClientVisitMenu.jpg\"></a>";
		if 	((getUserType(false)!= 4) || (getUserType(false) != 5)) {
			print "<a href=\"?page=visit_add&ClientID=$clientID\"><img border=\"0\" src=\"images/AddClientVisitMenu.jpg\"></a>";
			//print "<input type=\"image\" name=\"imageVisitPlus$i\" src=\"images/AddClientVisitMenu.jpg\">";
		}
		if 	((getUserType(false) == 2) || (getUserType(false) == 3)) {
			print "<a href=\"?page=client_edit&ClientID=$clientID\"><img border=\"0\" src=\"images/EditClientMenu.jpg\"></a>";
		}
		if 	(getUserType(false) == 2) {
			print "<input type=\"image\" name=\"imageVisitDele$i\" src=\"images/DeleteClientMenu.jpg\" onclick=\"if(!confirm('Are you sure to DELETE?')) return false\">";
		}
		if 	((getUserType(false) == 2) || (getUserType(false) == 3)) {
			print "<a href=\"?page=visit_view_all&ClientID=$clientID\"><img border=\"0\" src=\"images/AllClientVisitMenu.jpg\"></a>";
		}
		if 	((getUserType(false) == 2) || (getUserType(false) == 3)) {
			print "<a href=\"?page=comment_add&ClientID=$clientID\"><img border=\"0\" src=\"images/Comments.jpg\"></a>";
		}
		//print "<input type=\"image\" name=\"imageVisitPrin$i\" src=\"images/PrintClient.jpg\" >";
		print "<a href=\"javascript:void(OpenWindow('pages/form_new_visit.php?selectedClientID=$clientID','',710, 400));\"><img border=\"0\" src=\"images/PrintClient.jpg\" ></a>";
		//print "<input type=\"image\" name=\"imageVisitPrin$i\" src=\"images/PrintClient.jpg\" onclick=window.open('http://www.camcare.org.au/SSL/testNavVisit.php','','toolbar=no,menubar=no,location=no,height=500,width=500');  >";

		print "</td></tr></table><hr>";

		$totalGivenER=0;

		$resultPayment = getTotalAmountOnVisit($clientID)	;

		for($i=1;$i<=getNoOfVisits($clientID);$i++)	{
			while ($rowErGiven = mysql_fetch_array($resultPayment)) {
				$totalGivenER=$totalGivenER+$rowErGiven[2];
			}
		}

		$totalVisits=getNoOfVisits($clientID);

		$totalAdults=0;

		$totalChildren=0;

		$resultPayment = getTotalAdultsAndChildrenForClient($clientID)	;

		for($i=1;$i<=getNoOfVisits($clientID);$i++)

		{

			while ($rowErGiven = mysql_fetch_array($resultPayment))

			{
				$totalAdults=$totalAdults+$rowErGiven[3];

				$totalChildren=$totalChildren+$rowErGiven[2];

			}
		}

		$totalAdultER=0;

		$ERpriced=getAdultER();

		for($i=1;$i<=$totalAdults;$i++)
		{
			$totalAdultER=$ERpriced+$totalAdultER;
		}

		$totalChildER=0;

		$ERpriced1=getChildrenER();

		for($i=1;$i<=$totalChildren;$i++)

		{

			$totalChildER=$ERpriced1+$totalChildER;

		}
		$totalFamilyER=$totalAdultER+$totalChildER;

		ECHO "<table border=\"0\" width=\"100%\">";
		echo"<tr><td><h4>Client Eligibility</h4></td><td><h4>Client Comments</h4></centre></td></tr>";

		echo "<tr><td valign=\"top\" style=\"padding-left:10px;\">";
		echo "<table cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" border=\"0\">";

		//if(($totalGivenER >= $totalFamilyER) || ($totalVisits >= 5 ))
		if($totalVisits >= 4 )
		{

			echo "<tr><td colspan=\"2\" align=\"center\"><img border=\"0\" src=\"images/red_circle.jpg\" width=\"80\" heigth=\"80\" ></td></tr>";
			//$color="red";
		}
		elseif ($totalVisits == 3)
		{

			echo "<tr><td colspan=\"2\" align=\"center\"><img border=\"0\" src=\"images/orange_circle.jpg\" width=\"80\" heigth=\"80\" ></td></tr>";
		}
		else
		{
			echo "<tr><td colspan=\"2\" align=\"center\"><img border=\"0\" src=\"images/green.jpg\" width=\"80\" heigth=\"80\" ></td></tr>";
		}

		echo "<tr><td class=\"eligability_notice\">Total ER given in past year:</td><td class=\"eligability_value\">$ $totalGivenER</td></tr>";
		echo "<tr><td class=\"eligability_notice\">Total visits in past year:</td><td class=\"eligability_value\">$totalVisits</td></tr>";
		echo "</table>";

		echo "</td>";

		$arrCommentDetails = getCommentDetails($clientID);

		echo "<td width=\"50%\">";

		echo "<iframe src=\"pages/comment_view.php?clientID=$clientID&back=1\" width=\"100%\" frameborder=\"0\"></iframe>";

		echo "</td>";

		echo "</tr>";

		//echo"<input type=button onClick=\"window_open()\" value='View All Comments'>";

		echo"<script language=\"javascript\">



		function window_open()

		{

		var newWindow;

		var urlstring = 'viewallcomments.php'

		newWindow = window.open(urlstring,'','height=200,width=280,toolbar=no,minimize=no,status=no,memubar=no,location=no,scrollbars=no')

		}

		</script> ";

		echo "</tr>";

		echo "</table>";

		//CRITERIA ENDS+++++++++++++++++++++++++++++


		if (isset($clientID))
		{
			$query = "SELECT * FROM er_Client WHERE client_id ='$clientID'";
			$sql = r_db($query);
			$row = mysql_fetch_array($sql);

			$sql = "SELECT 		e.address, 
								e.postcode, 
								e.state, 
								e.suburb, 
								if(e.mobile_no=0 or e.mobile_no is NULL, '-', e.mobile_no) as mobile_no, 
								if(e.phone_no=0 or e.phone_no is NULL, '-', e.phone_no) as phone_no 
					FROM 		er_visit e 
					WHERE		client_id = $clientID 
					ORDER BY	visit_date desc limit 1";
			$previousVisitResult = r_db($sql);
			$previousVisitRow = mysql_fetch_array($previousVisitResult);

			$idtyperesult=getIDtype($row[10]);
			echo "<h4>Client Details</h4>";
			echo "<table border=\"0\" width=\"100%\"> ";
			echo  "<tr><td width=\"20%\"><b>Surname:</b><td width=\"30%\">$row[2]</td><td width=\"20%\"><b>Given Name:</b><td width=\"30%\">".$row[3]."</td><td></td></tr>";

			$DOB =  $row[4];
			$DbDOB=explode("-",$DOB);
			$dayDOB=$DbDOB[2];
			$monthDOB=$DbDOB[1];
			$yearDOB=$DbDOB[0];
			$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;
			if (strtolower($row[6])=="f")
				$gender = "Female";
			else
				$gender = "Male";
			echo  "<tr>"
			.	"<td><b>Date of Birth:</b><td>" . $DOB;
			echo "<td><b>Gender:</b><td>$gender</td><td>";
			echo  "</tr>";

			echo "<tr>"
			.	"<td><b>ID Type:</b><td>".$idtyperesult[0]. "</td>"
			.     "<td><b>ID Reference No:</b></td><td>" . $row[1]."</td>";
			echo "</tr>";
			$otheridvalue = $row[8];
			if ($otheridvalue != null)
			{
				echo   "<tr><td>&nbsp;</td><td>&nbsp;</td><td><b>Other ID Type:</b></td><td>" . $row[8]."</td></tr>";
			}


			echo "<tr>";
			echo "<td><b>Phone Number:&nbsp;</b><td>" . $previousVisitRow['phone_no']. "</td>";
			echo "<td><b>Mobile Number:&nbsp;</b><td>" . $previousVisitRow['mobile_no']. "</td>";
			echo "</tr>";

			echo "<tr><td><b>Last known address:</b></td><td>" . $previousVisitRow['address']. "</td><td><b>Last known suburb:</b></td><td> " . $previousVisitRow['suburb']. "</td></tr>";
			echo "<tr><td><b>Last known State:</b></td><td>" . $previousVisitRow['state']. "</td><td><b>Last known Postcode:</b></td><td>" . $previousVisitRow['postcode']. " </td></tr>";
			echo "</tr>";

			echo "</tr>";
			print "   <input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\">"; //</td>";
			print "   <input type=\"hidden\" name=\"selectedClientSurname\" value=\"". $row[2] . "\">";
			print "   <input type=\"hidden\" name=\"selectedClientFirstName\" value=\"". $row[3] . "\">";
			echo "</table>";
			echo "</body>";
			echo "</link></html>";
			print "<input type=\"hidden\" name=\"selectedClientID\" value=\"". $row[0] . "\">"; //</td>";

			echo

			"</table>";
		}

		print "</FORM>";
	}
}
else {
	echo "<h1 >$pageTitle</h1>";
	echo "No client has been accessed recently.  Please use the <a href=\"?page=search_basic\">search page</a> to find a client.";
}


//Functions start here++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++Functions

//view client functions by sunil

function getIDtype($idtype)

{

	$query="select identification_type from er_Identification_Type where identification_no='".$idtype."'";

	//$query="select identification_type from er_identification_type where identification_no='1'";

	$resultIDtype = r_db($query);



	$row=mysql_fetch_array($resultIDtype);



	return $row;



}



//##############COMMENTS##################

function getNoOfComments($clientID)

{

	$queryNoOfComments = "select count(*) from er_Comments where er_Comments.client_id='".$clientID."'";



	$resultNoOfComments = r_db($queryNoOfComments);



	$rowNoOfComments = mysql_fetch_array($resultNoOfComments);



	return $rowNoOfComments;

}



function getCommentDetails($clientID)

{

	$arr = array();



	$query="select * from er_Comments where er_Comments.client_id='".$clientID."'

						AND Year(er_Comments.comment_datetime) >= year(date_sub(now(),interval -1 year))

						order by rank";

	//echo $query;



	$results=r_db($query);



	while($row= mysql_fetch_object($results))

	{

		array_push($arr, $row);

	}



	return $arr;

}

//++++++++++++++++++++

function getComment($clientID)

{

	$queryComment = "select comment from er_Comments where er_Comments.client_id='".$clientID."'

						AND Year(er_Comments.comment_datetime) >= year(date_sub(now(),interval -1 year))

						order by rank";

	//

	$resultComment = r_db($queryComment);

	$rowNoOfComments = mysql_fetch_array($resultComment);

	return $rowNoOfComments;









}





function getCommentDate($clientID)

{

	$queryComment = "select comment_datetime from er_Comments where er_Comments.client_id='".$clientID."'

						AND Year(er_Comments.comment_datetime) >= year(date_sub(now(),interval -1 year))

						order by rank";

	//

	$resultComment = r_db($queryComment);

	$rowNoOfComments = mysql_fetch_array($resultComment);

	return $rowNoOfComments;

}



function getCommentRank($clientID)

{

	$queryComment = "select rank from er_Comments where er_Comments.client_id='".$clientID."'

						AND Year(er_Comments.comment_datetime) >= year(date_sub(now(),interval -1 year))

						order by rank";

	//

	$resultComment = r_db($queryComment);

	$rowNoOfComments = mysql_fetch_array($resultComment);

	return $rowNoOfComments;

}

//++++++++++++



function printCommentDetails($rowName,$arrComment,$position,$clientID)

{

	echo "<tr>";

	echo "<td>$rowName</td>";

	$getNoOfComments=getNoOfComments($clientID);

	for($i=1;$i<=$getNoOfComments[0];$i++)

	{

		while ($rowComment = mysql_fetch_array($arrComment))

		{

			echo "<td>$rowComment[$position]</td>";

		}

	}

	echo "</tr>";

}



//COMMENT FUNCTIONS ENDS

//CRITERIA FINCTIONS START

function getAdultER()

{

	$queryErGiven = "select criteria_value from er_Financial_Criteria where criteria_id='1'";



	$resultErGiven = r_db($queryErGiven);



	$rowER = mysql_fetch_array($resultErGiven);



	$noOfVisits = $rowER[0];

	return $noOfVisits;

}



function getChildrenER()

{

	$queryErGiven = "select criteria_value from er_Financial_Criteria where criteria_id='2'";



	$resultErGiven = r_db($queryErGiven);



	$rowER = mysql_fetch_array($resultErGiven);



	$noOfVisits = $rowER[0];

	return $noOfVisits;





}















function getTotalAdultsAndChildrenForClient($clientID)

{

	$queryErGiven = "SELECT er_visit.visit_id,er_visit.visit_date,

	er_visit.no_children_assistted, er_visit.no_adults_assistted

	FROM er_Client, er_visit

	WHERE er_Client.client_id=er_visit.client_id

	AND visit_date > date_sub(now(),interval 1 year)

	AND er_Client.client_id='$clientID' ";



	$resultErGiven = r_db($queryErGiven);



	return $resultErGiven;

}







function getTotalAmountOnVisit($clientID)

{
	global $sqlForYear;

	$queryErGiven = "SELECT er_Client.client_Id, er_visit.visit_id, SUM(payment_amount)

	FROM er_Client, er_visit, er_Payment_On_Visit

	WHERE er_Client.client_id=er_visit.client_id

	AND er_visit.visit_id=er_Payment_On_Visit.visit_id

	AND er_visit.visit_date >= ".$sqlForYear."

	AND er_Client.client_Id = '$clientID'

	GROUP BY er_visit.visit_id";


	$resultErGiven = r_db($queryErGiven);



	return $resultErGiven;

}



function getNoOfVisits($clientID)
{
	global $sqlForYear;
	$queryNoOfVisits = "SELECT er_Client.client_id, COUNT(er_visit.visit_id)
	FROM er_Client, er_visit
	WHERE er_Client.client_id=er_visit.client_id
	AND visit_date >= ".$sqlForYear."
	AND er_Client.client_id='$clientID'
	GROUP BY er_Client.client_id";

	$resultNoOfVisits = r_db($queryNoOfVisits);
	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	if (empty($noOfVisits))
		$noOfVisits = 0;
	return $noOfVisits;
}







function table_color($i)

{

	//$bg1 = "#B1CDC1"; // color one

	//$bg2 = "#E0EEE0"; // color two


	$bg1 = "RED"; // color one

	$bg2 = "ORANGE"; // color two

	$bg3 = "GREEN";





if ( $i==1 )

{

	return $bg1;

}

elseif($i==2)

{

	return $bg2;

}

else

{

	return $bg3;

}

}
//view client ends here
?>
